package com.li.sort.arithmetic;

import java.util.Arrays;
import java.util.Random;

public class BubbleSort {
	public static final int SIZE = 100000000;
	public static void main(String[] args) {
		int[] array = new int[SIZE];
		Random random = new Random();
		for(int i = 0;i < SIZE ;i++){
			array[i] = random.nextInt(100);
		}
		
		long startTime = System.currentTimeMillis();
		bubbleSort(array);
		for(int i=0;i<10;i++){
			System.out.println(array[SIZE-i-1]);
		}
		System.out.println("用时"+(System.currentTimeMillis()-startTime));
	}
	
	/**
	 * n-1趟排序就ok
	 * @param array
	 */
	public static void bubbleSort(int[] array){
		boolean isSort = true;
		for(int i = 1;i<10;i++){
			for(int j = 0;j < array.length-i;j++){
				if(array[j]>array[j+1]){ 
					isSort = false;
					swap(array,j,j+1);
				}
			}
			if(isSort == true){
				break;
			}
		}
	}
	
	public static void swap(int[] array,int i,int j){
		int temp = array[i];
		array[i] = array[j];
		array[j] = temp;
	}
}
